<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 */
namespace app\user\controller;

use  Mscore\Core\Controller;
use  app\user\model\Cert as CertModel;
use  app\admin\model\Login as LoginModel;
use  Mscore\Core\ArrayMethod;
use  Mscore\Core\Lang;

class CertController extends Controller
{

    
    private $model;
    private $login;
    private $error;
    private $success;
    /**
     * 初始化自动连接方法
     * @param array $sysmod 
     * @param array $where 
    */
    public function __construct()  
    {    
        $this->model = new CertModel;
        $this->login = (new LoginModel)->isAuth();
        if(isset($this->login['code'])&&$this->login['code']==401){
            echo json_encode($this->login);
            Header("Location:".config('server.login_url'));
            exit;
        }
        $this->language = isset($_COOKIE['language'])?$_COOKIE['language']:'default';
        Lang::init($this->language);
        $this->success=Lang::get('Success');
        $this->error=Lang::get('Error');
    }

    /**
     * 获取用户列表
     * @param array $userlist
     * @param array $where
     */
    public function list()
    {
        $post = post();
        if ($post) {
            $page = post('page',1);
            $limit = post('limit',10);
            $where = ' a.dels < 1 ';
            if($this->login['info']['ranges']<1){
                $where .= ' and (a.pid = '.$this->login['info']['aid'].' or a.aid='.$this->login['info']['aid'].')';
            }
            if(post('mobile')){
                $where .= ' and b.mobile like "%'.post('mobile').'%"';
            }
            if(post('realname')){
                $where .= ' and b.realname like "%'.post('realname').'%"';
            }
            if(post('username')){
               $where .= ' and b.username like "%'.post('username').'%"';
            }
            $datalist = $this->model->list($where,$page,$limit);
            $data = array(
                "code" => 200,
                "data" => $datalist['datalist'],
                "count"=> $datalist['total'],
                "msg" => 'ok'
            );
            $this->echojson($data);
        } else {
            $this->display('list.html');
        }

    }

    /**
     * 个人信息
     * @param array $info 
     * @param array $where 
     */ 
    public function info(){
        $params = post();
        if($params){
            //post请求则保存
            $datainfo = $this->model->infosave($params);
            switch ($datainfo) {
                case true:
                    $this->echoJson($this->success);
                    break;
                    
                default:
                    $this->echoJson($this->error);
                    break;
            }
        }else{
            if(get('id')){
                $info = $this->model->info(' a.uid='.get('id'));
            }
            $this->assign('datainfo', $info);
            $this->display('info.html');
        }
    }

    /**
     * 完善信息添加
     * @param array $add 
     * @param array $where 
     */ 
    public function add(){
       $params = post();
       if($params){
           //post请求则保存
           $datainfo = $this->model->save($params);
           switch ($datainfo) {
                case true:
                    $this->echoJson($this->success);
                    break;
                    
                default:
                    $this->echoJson($this->error);
                    break;
            }
       }else{
          if(get('id')){
              $userinfo = $this->model->info(' a.aid='.get('id'));
          }
          //定义变量userinfo输出
          $userinfo['aid']=isset($userinfo['aid'])?$userinfo['aid']:get('id');
          $userinfo['uid']=isset($userinfo['uid'])?$userinfo['uid']:'';
          $userinfo['realname']=isset($userinfo['realname'])?$userinfo['realname']:'';
          $userinfo['mobile']=isset($userinfo['mobile'])?$userinfo['mobile']:'';
          $userinfo['email']=isset($userinfo['email'])?$userinfo['email']:'';
          $userinfo['bankaccount']=isset($userinfo['bankaccount'])?$userinfo['bankaccount']:[['bank_name'=>'','bank_account'=>'','deposit_branch'=>'','deposit_name'=>'','fileurl'=>'','status'=>'',]];
          $userinfo['certificate']=isset($userinfo['certificate'])?$userinfo['certificate']:[];
          $this->assign('aid', get('id'));
          $this->assign('datainfo', $userinfo);
          $this->display('add.html');
       }
    }

    /**
     * 获取用户列表
     * @param array $userlist
     * @param array $where
     */
    public function banklist()
    {
        $post = post();
        if ($post) {
            $page = post('page',1);
            $limit = post('limit',10);
            $where = ' a.dels < 1 ';
            if($this->login['info']['ranges']<1){
                $where .= ' and (a.pid = '.$this->login['info']['aid'].' or a.aid='.$this->login['info']['aid'].')';
            }
            if(post('mobile')){
                $where .= ' and b.mobile like "%'.post('mobile').'%"';
            }
            if(post('realname')){
                $where .= ' and b.realname like "%'.post('realname').'%"';
            }
            if(post('username')){
                $where .= ' and b.username like "%'.post('username').'%"';
            }
            $datalist = $this->model->banklist($where,$page,$limit);
            $data = array(
                "code" => 200,
                "data" => $datalist['datalist'],
                "count"=> $datalist['total'],
                "msg" => 'ok'
            );
            $this->echojson($data);
        } else {
            $this->display('banklist.html');
        }
    }

    /**
     * 银行认证信息
     * @param array $bankinfo 
     * @param array $where 
     */ 
    public function bankinfo(){
        $params = post();
        if($params){
            //post请求则保存
            $datainfo = $this->model->bankinfosave($params);
            switch ($datainfo) {
                case true:
                    $this->echoJson($this->success);
                    break;
                    
                default:
                    $this->echoJson($this->error);
                    break;
            }
        }else{
            if(get('id')){
                $info = $this->model->bankinfo(' a.uid='.get('id'));
            }
            //定义变量userinfo输出
            $userinfo['uid']=isset($userinfo['uid'])?$userinfo['uid']:'';
            $userinfo['realname']=isset($userinfo['realname'])?$userinfo['realname']:'';
            $userinfo['mobile']=isset($userinfo['mobile'])?$userinfo['mobile']:'';
            $userinfo['email']=isset($userinfo['email'])?$userinfo['email']:'';
            $userinfo['bankaccount']=isset($userinfo['bankaccount'])?$userinfo['bankaccount']:[];
            $this->assign('aid', get('id'));
            $this->assign('datainfo', $info);
            $this->display('bankinfo.html');
        }
    }

}